package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
	"strconv"
	"strings"
)

func parseline(str string) (string, string, int, error) {
	elements := strings.Fields(str)

	codestats, err := strconv.Atoi(elements[8])
	if err != nil {
		return "", "", 0, err
	}
	return elements[0], elements[6], codestats, nil
}

func main() {
	file, err := os.Open("access.log")
	if err != nil {
		fmt.Println(err)
		return
	}
	defer file.Close()

	reader := bufio.NewReader(file)

	ipStats := make(map[string]int)
	urlstats := make(map[string]int)
	codestats := make(map[int]int)

	for {
		ctx, _, err := reader.ReadLine()
		if err != nil {
			if err == io.EOF {
				break
			}
			fmt.Println(err)
		}
		// fmt.Println(string(ctx))
		// fmt.Println(bytes.)
		ip, url, code, err := parseline(string(ctx))
		ipStats[ip]++
		urlstats[url]++
		codestats[code]++
	}
	fmt.Println(ipStats)
	fmt.Println(urlstats)
	fmt.Println(codestats)

	// io.Copy(os.Stdout, file)
}
